//
//  ViewController.swift
//  PageViewDemo
//
//  Created by qings on 16/1/24.
//  Copyright © 2016年 pukaila. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    var screenObject:CGRect=UIScreen.mainScreen().bounds
    var label:UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        intView()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func intView() {
        createPageView()
        createContentView()
    }

    func createContentView() {
        label = UILabel(frame: CGRectZero)
        label.text = "1"
        label.font = UIFont(name: "AmericanTypewriter-Bold", size: CGFloat(120))
        label.textAlignment = NSTextAlignment.Center
        self.view.addSubview(label)
 
        label.translatesAutoresizingMaskIntoConstraints = false
        let constraintX:NSLayoutConstraint = NSLayoutConstraint(
            item: label, attribute: .CenterX, relatedBy: .Equal,
            toItem: self.view, attribute: .CenterX, multiplier: 1.0, constant: 0.0)
        self.view.addConstraint(constraintX)
        let constraintY:NSLayoutConstraint = NSLayoutConstraint(
            item: label, attribute: .CenterY, relatedBy: .Equal,
            toItem: self.view, attribute: .CenterY, multiplier: 1.0, constant: 0.0)
        self.view.addConstraint(constraintY)
    }
    
    func createPageView() {
        let height = 40
        let width = screenObject.width
        let x = 0
        let y = screenObject.height - CGFloat(height)
        let pageView : PageView = PageView(frame: CGRectMake(CGFloat(x), CGFloat(y), CGFloat(width), CGFloat(height)))
        pageView.currentPageNum = 1
        pageView.pageScrollViewPageSize = 6
        pageView.pageScrollViewPageNumHeight = 40
        pageView.pageScrollViewPageNumWidth = 40
        pageView.pageScrollViewPageNumSpacing = 1
        pageView.nextButtonWidth = 44
        pageView.prevButtonWidth = 44
        
        pageView.doInit()
        
        pageView.callback={(page)->Void in
            self.updatePage(page)
        }
        pageView.prevBtnCallback={(page)->Void in
            self.updatePage(page)
        }
        pageView.nextBtnCallback={(page)->Void in
            self.updatePage(page)
        }
        self.view.addSubview(pageView)
    }
    
    func updatePage(page:Int)->Void {
        self.label.text = String(page)
    }
}

